/**
 * @copyright Copyright 2009 Structured Solutions
 * @license http://www.codeplex.com/ShippingAgent/license Microsoft Public License (Ms-PL)
 * @version 3.2
 */

using System;
using System.Text.RegularExpressions;

namespace StructuredSolutions.ShippingAgent
{
	public class ShippingAgentHelper
	{
		public static bool ConvertToBoolean(object value)
		{
			if (value == null) return false;
			return value == Convert.DBNull ? false : Convert.ToBoolean(value);
		}

		public static DateTime ConvertToDateTime(object value)
		{
			if (value == null) return DateTime.MinValue;
			return value == Convert.DBNull ? DateTime.MinValue : Convert.ToDateTime(value);
		}

		public static string ConvertToString(object value)
		{
			if (value == null) return string.Empty;
			return value == Convert.DBNull ? string.Empty : Convert.ToString(value);
		}

		public static string RemoveHtmlTags(string value)
		{
			return Regex.Replace(value, @"</?(\w+)*|/?>", string.Empty).Replace("&nbsp;", " ").Replace("  ", " ");
		}

		public static string StringOrNull(string value)
		{
			if (value == null)
				return null;

			string trimmed = value.Trim();
			if (trimmed == string.Empty)
				return null;

			return trimmed;
		}

		public static bool StringsAreEqual(string s1, string s2, bool ignoreCase)
		{
			string trimmed1 = StringOrNull(s1);
			string trimmed2 = StringOrNull(s2);
			if (trimmed1 == null && trimmed2 == null)
				return true;
			return string.Compare(s1, s2, ignoreCase) == 0;
		}
	}
}
